<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.code" clearable class="filter-item" style="width: 200px;" placeholder="请输入主诉编码" />
      <el-input v-model="listQuery.content" clearable class="filter-item" style="width: 200px;" placeholder="请输入主诉内容" />
      <el-button v-permission="['GET /admin/complaint/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" >查找</el-button>
      <el-button v-permission="['POST /admin/complaint/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="ID" prop="id" sortable width="120"/>

      <el-table-column align="left" label="编码" prop="code" width="180"/>
      <el-table-column align="left" label="内容" prop="content" />
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button v-permission="['GET /admin/complaint/diagnosis/list']" type="primary" size="mini" @click="handleDiagnosisList(scope.row)">诊断</el-button>
          <el-button v-permission="['POST /admin/complaint/update']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['POST /admin/complaint/delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { listComplaint, deleteComplaint } from "@/api/complaint"
import Pagination from "@/components/Pagination"

export default {
  name: "ComplaintList",
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        code: undefined,
        content: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listComplaint(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/complaint/create' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/complaint/edit', query: { id: row.id }})
    },
    handleDiagnosisList(row) {
      this.$router.push({ path: '/complaint/diagnosis', query: { id: row.id }})
    },
    handleDelete(row) {
      this.$confirm('删除主诉, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteComplaint(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除药品成功'
            })
            this.getList()
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
      }).catch()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['ID', '编码', '内容']
        const filterVal = ['id', 'code', 'content']
        excel.export_json_to_excel2(
          tHeader,
          this.list,
          filterVal,
          '主诉信息'
        )
        this.downloadLoading = false
      })
    }
  }

}
</script>

<style scoped>

</style>
